import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.InputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.StringTokenizer;


public class testingexec {
	public byte[] actualTask(int n){
		//int n=4;
		File f=null;
		byte[] bytes=new byte[5];
		BufferedReader br=null;
		String line=null;
		double totalcount=0;
		double totalsum=0;
		Runtime runtime = Runtime.getRuntime();
		Process p=null;
		Process pp=null;
		try{
			/*String s=System.getProperty("os.name");
			if(s.startsWith("W"))
			   p=runtime.exec("./run.bat");
			if(s.startsWith("L"))
			   p=runtime.exec("./run");
			
			p.waitFor();
			p.destroy();*/
			for(int i=0;i<n;i++){
				pp=runtime.exec("./Cprogram"+" "+i+" "+((i*100)+i)+" "+"2");	
			}
			
		}catch(Exception e){
			System.out.println("exception occured");
		}
		System.out.println("all jobs launched");
		ArrayList al=new ArrayList();
		for(int i=0;i<n;i++){
        	al.add("task"+i);
        }
		 while(al.size()!=0){
	        	System.out.println("into while loop");
	        	System.out.println("size is:"+al.size());
	        	for(int i=1;i<=al.size();i++){
	        		System.out.println("into for loop");
	        		String s="./"+(String)(al.get(i-1))+".txt";
	        		f=new File(s); 
	        		System.out.println("the file processing is:"+f);
	        		 //if(f.exists()){
	        			  /*System.out.println("file:"+f+" "+"exists");
	        			 
	        			  try{
	        				 br= new BufferedReader(new FileReader(f));
	        				 line=br.readLine();
	        				 System.out.println("line is:"+line);
	        				 if(line==null)
	        				 break;
	                      }
	        			  catch(Exception e){
	        				  System.out.println("exception occured while creating streams to read the files");
	        			  }
	        			  StringTokenizer st=new StringTokenizer(line," ");
	        			  StringTokenizer st1=new StringTokenizer(st.nextToken(),"=");
	        			  st1.nextToken();
	        			  totalcount=totalcount+Integer.parseInt(st1.nextToken());
	        			  st1=new StringTokenizer(st.nextToken(),"=");
	        			  st1.nextToken();
	        			  totalsum=totalsum+Integer.parseInt(st1.nextToken());*/
	        			  al.remove(i-1);
	        		//}
	        		//else
	        			//System.out.println("file doesnot exist");
	        	   }
		         }
	        	 System.out.println("all the files are ready");
	             try{
	             br.close();
	             }catch(Exception e){
	             	System.out.println("exception occured while closing br");
	             }
	              System.out.println("creating the final file");
	              f=new File("./finalresults.txt");
	              try{
	              PrintStream ps = new PrintStream(new FileOutputStream(f));
	              ps.println("count=");
	              ps.println(totalcount);
	              ps.println("sum=");
	              ps.println(totalsum);
	              ps.close();
	              
	              /*InputStream is = new FileInputStream(f);
	              int length =(int) f.length();
	              bytes = new byte[(int)length];
	              is.read(bytes,0,length);
	              is.close();*/
            
	              }catch(Exception e){
	             	 System.out.println("exception occured while writing final results to the file");
	              }
	              bytes[0]=10;
	              bytes[1]=20;
	              return bytes;
	        }
	}


